package com.JUC.cha03.sec06;


import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * 5. 对计算结果进行合并
 *  5.1. thenCombine
 */
public class CompletableFutureAPI5Demo {

    public static void main(String[] args) {

        CompletableFuture<Integer> resultA = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t come in");
            // 暂停几秒钟线程
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 10;
        });

        CompletableFuture<Integer> resultB = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t come in");
            // 暂停几秒钟线程
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 20;
        });

        CompletableFuture<Integer> result = resultA.thenCombine(resultB, (a, b) -> {
            System.out.println("come in thenCombine");
            return a + b;
        });

        System.out.println(result.join());

    }
}
